<?php

class ContactController extends Zend_Controller_Action 
{
    public function indexAction()
    {
    	$form = new Default_Form_Contact('');
		$this->view->form = $form;
		
		if($this->getRequest()->isPost())
		{
			try
			{
				$data = $this->_request->getPost();
				if($form->isValid($data))
				{
					$userData = array(
						'Email' => $data['email'],
						'Subject' => $data['subject'],
						'Message' => $data['message'],
					);
					$contactMail = new Default_Model_Contactmail($userData);
					
					$mail = new Zend_Mail('UTF-8');
					$mail->setBodyText(Default_Model_Validator::filterText($contactMail->getMessage()));
					$mail->setFrom($contactMail->getEmail(), 'Spagooy Contact us');
					$mail->addTo('robinbengtsson88@gmail.com', 'Spagooy');
					$mail->setSubject(Default_Model_Validator::filterText($contactMail->getSubject()));						
					$mail->send();
					
					Zend_Registry::get('userLog')->logusers('New contact mail sent: Email: ' . $contactMail->getMessage() . ' Subject: ' . $contactMail->getSubject() . ' Message: ' . $contactMail->getMessage());
					$this->_helper->getHelper('FlashMessenger')->addMessage(array('message' => Zend_Registry::get('translate')->_('CONTACT_US_MESSAGE_SENT'),'status' => 'success'));
				}
			}
			catch(Exception $e)
			{
				Zend_Registry::get('errLog')->err('Could not send "contact us mail".');
				$this->_helper->getHelper('FlashMessenger')->addMessage(array('message' => Zend_Registry::get('translate')->_('CONTACT_US_MESSAGE_ERROR'),'status' => 'warning'));
			}
		}
    }
}
